/**
 * Program  : IntroActivity.java
 * Author   : long
 * Create   : 2015年12月29日 下午4:00:06
 *
 * Copyright 2015 by Embedded Internet Solutions Inc.,
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Embedded Internet Solutions Inc.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with Embedded Internet Solutions Inc.
 *
 */

package cn.broadin.app.hotel;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import cn.broadin.app.hotel.app.AppContext;
import cn.broadin.app.hotel.util.XHttpProxy;
import cn.broadin.app.hotel.util.XLog;
import cn.broadin.app.hotel.util.XToast;
import cn.broadin.app.hotel.util.XUtils;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;

/**
 * 旅馆介绍界面
 * 
 * @author long
 * @version 1.0.0
 * @date 2015年12月29日下午4:00:06
 */
public class IntroActivity extends BaseActivity {
	
	private List<String> photos;
	int index = 0;
	int totalPhotos = 0;
	
	private ImageView photoImageView01;
	private ImageView photoImageView02;
	private ImageView photoImageView03;
	private ImageView photoImageView04;
	private ImageView photoImageView05;
	private ImageView[] photosImageViews;
	
	private ImageView actionLeft;
	private ImageView actionRight;
	
	private String loadPhotoUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);
		
		loadPhotoUrl = getIntent().getStringExtra("url");
		XLog.d("loadPhotoUrl: " + loadPhotoUrl);
		
		initLoadPhotos();
	}

	/**
	 * 加载服务器照片数据
	 * 
	 * @author   long
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2015年12月29日 下午7:31:33
	 */
	private void initLoadPhotos(){
		showLoadingDialog();
		RequestParams requestParams = new RequestParams();
		HttpUtils httpUtils = XHttpProxy.getHttpUtils();
		httpUtils.send(HttpRequest.HttpMethod.POST, loadPhotoUrl, requestParams, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				hideLoadingDialog();
				XToast.makeText(IntroActivity.this, "服务器繁忙，请稍后再试！").show();
			}

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				XLog.i("responseInfo.result: " + responseInfo.result);
				photos = (List<String>) JSON.parse(responseInfo.result);
				initView();
				hideLoadingDialog();
			}
		});
	}
	
	private void initView() {
		
		photosImageViews = new ImageView[5];
		
		Matrix matrix = new Matrix();
		int dx = (int) XUtils.getDimension(this, R.dimen.px36);
		int dy = (int) XUtils.getDimension(this, R.dimen.px32);
		matrix.setTranslate(dx, dy);
		
		photoImageView01 = (ImageView) findViewById(R.id.photo_01);
		photoImageView01.setImageMatrix(matrix);
		int leftMargin = (int) XUtils.getDimension(this, R.dimen.px20);
		FrameLayout.LayoutParams layoutParams = (LayoutParams) photoImageView01.getLayoutParams();
		layoutParams.leftMargin = (0 - leftMargin);
		photoImageView01.setLayoutParams(layoutParams);
		photosImageViews[4] = photoImageView01;
		
		photoImageView02 = (ImageView) findViewById(R.id.photo_02);
		photoImageView02.setImageMatrix(matrix);
		photosImageViews[3] = photoImageView02;
		
		photoImageView03 = (ImageView) findViewById(R.id.photo_03);
		photoImageView03.setImageMatrix(matrix);
		photosImageViews[2] = photoImageView03;
		
		photoImageView04 = (ImageView) findViewById(R.id.photo_04);
		photoImageView04.setImageMatrix(matrix);
		photosImageViews[1] = photoImageView04;
		photoImageView04.setImageMatrix(matrix);
		
		photoImageView05 = (ImageView) findViewById(R.id.photo_05);
		photoImageView05.setImageMatrix(matrix);
		photosImageViews[0] = photoImageView05;
		
		// 箭头显示
		actionLeft = (ImageView) findViewById(R.id.action_left);
		actionLeft.setVisibility(View.VISIBLE);
		actionRight = (ImageView) findViewById(R.id.action_right);
		actionRight.setVisibility(View.VISIBLE);
		
		initPhotoView();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getAction() == KeyEvent.ACTION_DOWN){
			// 焦点位于旅馆介绍  按123进入参数配置页面
			switch(event.getKeyCode()){
				case KeyEvent.KEYCODE_DPAD_LEFT:
					index --;
					initPhotoView();
					AppContext.sound.moveFocusLeft();
					break;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					index ++;
					initPhotoView();
					AppContext.sound.moveFocusLeft();
					break;
			}
		}
		return super.dispatchKeyEvent(event);
	}
	
	/**
	 * 刷新图片数据
	 * 
	 * @author   long
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2015年12月30日 上午11:02:07
	 */
	private void initPhotoView(){
		showLoadingDialog();
		totalPhotos = photos.size();
		if(index < 0){
			index = totalPhotos - 1;
		}
		int max = Math.min(totalPhotos, 5);
		for(int i = 0; i < max; i ++){
			ImageView imageView = photosImageViews[i];
			int tmpIndex = (i + index) % totalPhotos;
			String url = photos.get(tmpIndex);
			loadBitmap(imageView, url);
		}
		hideLoadingDialog();
	}
	
	/**
	 * 加载bitmap图片
	 * 
	 * @author   long
	 * @param imageView
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2015年9月23日 下午4:32:44
	 */
	public void loadBitmap(final ImageView imageView, String uri){
		BitmapUtils bitmapUtils = new BitmapUtils(getApplicationContext());
		BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
		bitmapUtils.configDefaultDisplayConfig(displayConfig);
		bitmapUtils.display(imageView, uri);
	}
}
